package com.sl.biz.modular.ai.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.HMac;
import com.google.common.collect.Maps;
import com.sl.biz.modular.ai.dao.domain.entity.AiChatConfigDO;
import com.sl.biz.modular.ai.dao.service.AiChatConfigDaoService;
import com.sl.biz.modular.ai.service.AiChatConfigBizService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.beetl.core.*;
import org.beetl.core.resource.StringTemplateResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URI;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class AiChatConfigBizServiceImpl implements AiChatConfigBizService {

    private final AiChatConfigDaoService aiChatConfigDaoService;

    private static final GroupTemplate GROUP_TEMPLATE;

    static {
        Configuration cfg = null;
        try {
            cfg = Configuration.defaultConfiguration();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        cfg.setPlaceholderStart("${");
        cfg.setPlaceholderEnd("}");
        cfg.setStatementStart("<!--:");
        cfg.setStatementEnd("-->");
        cfg.setStatementStart2("/**:");
        cfg.setStatementEnd2("**/");
        cfg.setNativeCall(false);
        cfg.setSafeOutput(true);
        cfg.setStrict(false);
        cfg.build();
        GROUP_TEMPLATE = new GroupTemplate(new StringTemplateResourceLoader(), cfg);
        GROUP_TEMPLATE.registerFunction("urlPath", new UrlFunc());
        GROUP_TEMPLATE.registerFunction("sha", new Sha());
        GROUP_TEMPLATE.registerFunction("base64", new Base64());
        GROUP_TEMPLATE.registerFunction("urlEncode", new UrlEncode());

    }

    public static class UrlFunc implements Function {

        @SneakyThrows
        public String call(Object[] paras, Context ctx) {
            Object o = paras[0];
            URI uri = URLUtil.toURI(o.toString());
            return uri.getPath();
        }
    }
    public static class Base64 implements Function {

        @SneakyThrows
        public String call(Object[] paras, Context ctx) {
            Object o = paras[0];
            return cn.hutool.core.codec.Base64.encode(o.toString());
        }
    }

    public static class UrlEncode implements Function {

        @SneakyThrows
        public String call(Object[] paras, Context ctx) {
            Object o = paras[0];
            return URLUtil.encode(o.toString());
        }
    }

    public static class Sha implements Function {

        @SneakyThrows
        public String call(Object[] paras, Context ctx) {
            Object o = paras[0];
            String signatureOrigin = paras[1].toString();
            HMac hMac = SecureUtil.hmacSha256(signatureOrigin);
            String s = hMac.digestBase64(o.toString(), false);
            return s;
        }
    }

    @Override
    public Boolean save(AiChatConfigDO cdcConfigDO) {
        return aiChatConfigDaoService.saveOrUpdate(cdcConfigDO);
    }

    @Override
    public AiChatConfigDO getByAiId(AiChatConfigDO cdcConfigDO) {
        return aiChatConfigDaoService.getByAiId(cdcConfigDO.getAiId());
    }

    @SneakyThrows
    @Override
    public String resolveTemplateConfig(String template, Map<String, Object> data) {

        if (ObjUtil.isNull(data)) {
            data = Maps.newHashMap();
        }

        Template template1 = GROUP_TEMPLATE.getTemplate(template);
        template1.binding(data);
        String render = template1.render();
        return render;
    }
}
